package com.huawei.fakedouyin.adapter;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.graphics.Outline;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.fakedouyin.R;
import com.huawei.fakedouyin.data.VideoInfo;
import com.huawei.fakedouyin.manager.PlayManager;
import com.huawei.fakedouyin.service.SocketService;
import com.huawei.fakedouyin.util.ActivityUtil;
import com.huawei.fakedouyin.util.LiveDataSet;
import com.huawei.fakedouyin.util.LogUtil;
import com.huawei.fakedouyin.util.LogUtilKt;
import com.huawei.fakedouyin.util.ScreenUtils;
import com.huawei.fakedouyin.util.ThreadPool;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 视频列表适配器
 */
public class PagerSnapHelperAdapter extends RecyclerView.Adapter<PagerSnapHelperAdapter.ViewHolder> {

    // 数据集
    private ArrayList<VideoInfo> mDataList = new ArrayList<>();

    public HashMap<Integer, PagerSnapHelperAdapter.ViewHolder> viewHolderHashMap = new HashMap<>();
    public onItemClickListener onItemClickListener;

    //更新数据
    public void update(ArrayList<VideoInfo> list) {
        this.mDataList = list;
        this.notifyDataSetChanged();
    }

    public PagerSnapHelperAdapter(ArrayList<VideoInfo> dataset, int width, int height) {
        super();
        this.mDataList = dataset;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        LogUtilKt.logDebug("onCreateViewHolder");
        View view = View.inflate(viewGroup.getContext(), R.layout.video_item, null);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        // 创建一个ViewHolder
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") int position) {
        LogUtilKt.logDebug("onBindViewHolder position:" + position);
        // 绑定数据到ViewHolder上
        viewHolder.itemView.setTag(position);
        viewHolder.contentView.removeAllViews();
        viewHolderHashMap.put(position, viewHolder);
        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemLongClick(position);
                }
                return true;
            }
        });
        //视频介绍
        viewHolder.videoName.setText(mDataList.get(position).getVideoName());
        //强制圆形
        viewHolder.photo1.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        });
        viewHolder.photo1.setClipToOutline(true);
        viewHolder.photo1.setImageDrawable(ActivityUtil.Companion.getInstance().getCurrentActivity().getDrawable(R.drawable.photo_simple));


        viewHolder.photo2.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        });
        viewHolder.photo2.setClipToOutline(true);
        //自动旋转
        viewHolder.photo2.setImageDrawable(ActivityUtil.Companion.getInstance().getCurrentActivity().getDrawable(R.drawable.play_music));
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(viewHolder.photo2, "rotation", 0, 359);
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        objectAnimator.setDuration(5000);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();

        //开线程加载封面图 第一次加载的话 自动开始播放第一个视频
        ThreadPool.getInstance().run(new Runnable() {
            @Override
            public void run() {
                //图片加载成功后 自动从第一个item开始播放
                PlayManager.getInstance().createVideoThumbnail(mDataList.get(position).getVideoUrl(),
                        viewHolder.largePicture, viewHolder.itemView,
                        (position == 0 && !PlayManager.getInstance().isInit));
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        //切换视频
        LogUtil.Companion.getInstance().logDebug("onViewDetachedFromWindow:" + holder.itemView.getTag());
        if (holder.itemView.getTag().toString().equals("0")) {
            if (mDataList.size() > 0 && PlayManager.getInstance().getBitmap(mDataList.get(0).getVideoUrl()) != null) {
                holder.largePicture.setImageBitmap(PlayManager.getInstance().getBitmap(mDataList.get(0).getVideoUrl()));
            }
        }
        holder.largePicture.setVisibility(View.VISIBLE);
        //退出观看此视频 发送socket消息
        SocketService.updateWatchStatus(false);
        if (mDataList.size() > 0) {
            LiveDataSet.watchMsg.postValue(mDataList.get(Integer.parseInt(holder.itemView.getTag().toString())).getVideoId());
            //说明该item消失 被划过去了
            PlayManager.getInstance().onDetach(mDataList.get(Integer.parseInt(holder.itemView.getTag().toString())).getVideoUrl());
        }

    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView largePicture;
        public ImageView photo1;
        public ImageView photo2;
        public RelativeLayout contentView;
        public TextView videoName;

        public ViewHolder(View itemView) {
            super(itemView);
            largePicture = itemView.findViewById(R.id.largePicture);
            contentView = itemView.findViewById(R.id.contentView);
            photo1 = itemView.findViewById(R.id.photo);
            photo2 = itemView.findViewById(R.id.photoBottom);
            videoName = itemView.findViewById(R.id.videoName);

        }
    }

    public interface onItemClickListener {
        void onItemLongClick(int position);
    }

    public void setOnItemClickListener(onItemClickListener clickListener) {
        this.onItemClickListener = clickListener;
    }

}
